﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BDShop.Entities;
using BDShop.DAL;
using IsolationLevel = System.Transactions.IsolationLevel;
using BDShop.Business.Cache;

namespace BDShop.Business
{
    public class ProductService 
    {
        public static List<ProductCacheComplex> GetListProductCacheModel()
        {
            List<ProductCacheComplex> lstProduct = new List<ProductCacheComplex>();
            using (var context = new BDShopEntities(IsolationLevel.ReadUncommitted))
            {
                lstProduct = context.Database.SqlQuery<ProductCacheComplex>("SELECT Id,Name,ShortDesc,Price,DiscountPrice,MarketPrice,MainImage,IsFeatured,Status,CategoryDefinition,DateCreated,SellTotal,ViewTotal,Rating,Rater,SupplierID,BrandID FROM Product").ToList();
            }
           
            return lstProduct;
        }

        public static List<ProductCategory> GetCategoryOfProduct(long id)
        {
            List<ProductCategory> lstProductCategory = new List<ProductCategory>();
            using (var context = new BDShopEntities(IsolationLevel.ReadUncommitted))
            {
                lstProductCategory = context.ProductCategories.ToList();
            }

            return lstProductCategory;
        }

        /// <summary>
        /// Get product by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Product GetProductByID(long id)
        {           
            Product product=new Product();
            using (var context = new BDShopEntities(IsolationLevel.ReadUncommitted))
            {
                product = context.Products.Where(p => p.ID == id).Single();
            }

            return product;
        }

        public static bool Update(long id,int rating)
        {
            try
            {
                Product product = new Product();
                using (var context = new BDShopEntities(IsolationLevel.ReadUncommitted))
                {
                    product = context.Products.Where(p => p.ID == id).Single();
                    product.Rating += rating;
                    product.Rater += 1;

                    context.SaveChanges();
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
           
        }

        public static bool UpdateSellQuantity(long id, int qyt)
        {
            try
            {
                Product product = new Product();
                using (var context = new BDShopEntities(IsolationLevel.ReadUncommitted))
                {
                    product = context.Products.Where(p => p.ID == id).Single();
                    product.SellTotal += qyt;

                    context.SaveChanges();
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

        public static bool UpdateView(long id)
        {
            try
            {
                Product product = new Product();
                using (var context = new BDShopEntities(IsolationLevel.ReadUncommitted))
                {
                    product = context.Products.Where(p => p.ID == id).Single();
                    product.ViewTotal += 1;

                    context.SaveChanges();
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }
    }
}
